<?php
class FormAction extends Action{
    public function insert(){
        $Form   =   D('Form');
        if($Form->create()) {
            $result =   $Form->add();
            if($result) {
                $this->success('操作成功！');
            }else{
                $this->error('写入错误！');
            }
        }else{
            $this->error($Form->getError());
        }
    }
	public function read($id=0){
	    $Form   =   M('Form');
	    // 读取数据
	    $data =   $Form->find($id);
	    if($data) {
	        $this->data =   $data;// 模板变量赋值
	    }else{
	        $this->error('数据错误');
	    }
	    $this->display();
	}
	
	public function edit($id=0){
	    $Form   =   M('Form');
	    $this->vo   =   $Form->find($id);
	    $this->display();
	}
	public function update(){
	    $Form   =   D('Form');
	    if($Form->create()) {
	        $result =   $Form->save();
	        if($result) {
	            $this->success('操作成功！');
	        }else{
	            $this->error('写入错误！');
	        }
	    }else{
	        $this->error($Form->getError());
	    }
	}
    public function insert2(){
        $Form   =   M('User');
        if($Form->create()) {
        	$Form->regdate = time();
        	$Form->birthday=strtotime($Form->birthday);
            $result =   $Form->add();
            if($result) {
                $this->success('操作成功！');
            }else{
                $this->error('写入错误！');
            }
        }else{
            $this->error($Form->getError());
        }
    }
    public function login2(){
    	//$User = M('User');
    	$Form = M('User');
    	if($Form->create())
    	{
    		$condition['email'] = $Form->email;
    		$condition['password'] = $Form->password;
    		$user = $Form->where($condition)->select();
    		//$username = $User->email;
    		if(count($user)!=0)
    		{
    			//$this->success('欢迎'.$user[0]['username']);
    			$_SESSION['email'] = $user[0]['email'];
				$_SESSION['userid'] = $user[0]['uid'];
				$this->assign("user",$user);
    			Header("Location:index");
    		}
    			
    		else 
    			$this->error('操作失败！');	
    	}else{
    		$this->error($Form->getError());
    	}
    }
	public function index($id=0){
		if(!isset($_SESSION['userid'])){
		header("Location: login");
		exit;
		}
		$userid = $_SESSION['userid'];
	    $User   =   M('User');
	    // 读取数据
	    $data =   $User->find($userid);
	    if($data) {
	        $this->data = $data;// 模板变量赋值
	        $this->userid = $userid;
	    }else{
	        $this->error('数据错误');
	    }
	    $this->display();
	}
	
	public function doajaxfileupload(){
			ini_set("error_reporting","E_ALL & ~E_NOTICE");
			session_start();
			if(!isset($_SESSION['userid'])){
				header("Location: login.html");
				exit;
			}
			
			$userid=$_SESSION['userid'];
			
			$error = "";
			$msg = "";
			$fileElementName = 'pic';
			if ((($_FILES["pic"]["type"] != "image/gif")
		&& ($_FILES["pic"]["type"] != "image/jpeg")
		&& ($_FILES["pic"]["type"] != "image/pjpeg")
		&& ($_FILES["pic"]["type"] != "image/bmp")
		&& ($_FILES["pic"]["type"] != "image/png"))
		|| ($_FILES["pic"]["size"] > 2000000)){
				$error="请选择2M以下的图片文件(gif,jpg,jpeg,bmp,png)";
				echo "{";
				echo				"error: '" . $error . "',\n";
				echo				"msg: '" . $msg . "'\n";
				echo "}";
				exit(0);
		}
			if(!empty($_FILES[$fileElementName]['error']))
			{
				switch($_FILES[$fileElementName]['error'])
				{
		
					case '1':
						$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
						break;
					case '2':
						$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
						break;
					case '3':
						$error = 'The uploaded file was only partially uploaded';
						break;
					case '4':
						$error = 'No file was uploaded.';
						break;
		
					case '6':
						$error = 'Missing a temporary folder';
						break;
					case '7':
						$error = 'Failed to write file to disk';
						break;
					case '8':
						$error = 'File upload stopped by extension';
						break;
					case '999':
					default:
						$error = 'No error code avaiable';
				}
			}elseif(empty($_FILES['pic']['tmp_name']) || $_FILES['pic']['tmp_name'] == 'none')
			{
				$error = 'No file was uploaded..';
			}else 
			{
					$extend = explode(".",$_FILES["pic"]["name"]);
				    $key = count($extend)-1;
				    $ext = ".".$extend[$key];
				    $newfile = $userid.$ext;
				    $newfileori = $userid."_ori".$ext;
				 
				    if(!file_exists('picture')){mkdir('picture');}
				    if (file_exists($newfile))
				    {
				      unlink($newfile);
				    }
					if (file_exists($newfileori))
				    {
				      unlink($newfileori);
				    }
				    move_uploaded_file($_FILES["pic"]["tmp_name"],"picture/" . $newfileori);
				    
				    require_once ('Lib/Model/image.php');
				    $img = new Image("picture/" . $newfileori);
				    $img->changeSize(300);//改变尺寸
					$img->create("picture/" . $newfile);
					$img->free();
					
					$msg = 'picture/'.$newfile;
					@unlink($_FILES['pic']);		
			}		
			echo "{";
			echo				"error: '" . $error . "',\n";
			echo				"msg: '" . $msg . "'\n";
			echo "}";
	}
}
?>